package cn.tedu.tmallpassport.controller;

import cn.tedu.tmall.common.pojo.authentication.CurrentPrincipal;
import cn.tedu.tmall.common.web.JsonResult;
import cn.tedu.tmallpassport.pojo.param.UserLoginInfoParam;
import cn.tedu.tmallpassport.pojo.vo.UserLoginResultVO;
import cn.tedu.tmallpassport.service.IUserService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Currency;

@RestController
@RequestMapping("/users")
@Api(tags = "单点登录")
public class UserController {
    @Autowired
    IUserService service;

    @PostMapping("/login")
    @ApiOperation("用户登录")
    @ApiOperationSupport(order = 20)
    public JsonResult login(@Validated UserLoginInfoParam userLoginInfoParam,
                            @ApiIgnore HttpServletRequest request) {
        // 通过HttpServletRequest获取IP与浏览器信息，并将这2个数据传递给Service组件
        String remoteAddr = request.getRemoteAddr();// IP地址
        String userAgent = request.getHeader("User-Agent");// 浏览器信息
        UserLoginResultVO loginResult = service.login(userLoginInfoParam,remoteAddr,userAgent);
        return JsonResult.ok(loginResult);
    }

    @PostMapping("/logout")
    @ApiOperation("退出登录")
    @ApiOperationSupport(order = 200)
    public JsonResult logout(@ApiIgnore @AuthenticationPrincipal CurrentPrincipal currentPrincipal){
        service.logout(currentPrincipal);
        return JsonResult.ok();

    }


}
